using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Xml;
using Graffiti.Core;
using System.Web;

namespace Graffiti.Slide.Show
{
    /// <summary>
    /// Common helper methods used by all of the plugins
    /// </summary>
    internal class PluginHelper
    {

        #region Default XML Values

        internal static readonly string defaultModulesXML = "<module type=\"SlideViewer\" />\n"
            + "<module type=\"ProgressPie\" />\n"
            + "<module type=\"SlideDescription\">\n"
            + "<option name=\"hideIfEmpty\" value=\"true\" />\n"
            + "</module>\n"
            + "<module type=\"NavigationTray\">\n"
            + "<option name=\"initialAlbumView\" value=\"true\" />\n"
            + "</module>\n";

        #endregion

        internal static void AddOptionFormElements(FormElementCollection fec)
        {
            fec.Add(new MessageElement("basicOptions", "<br /><strong>Basic Options</strong>", ""));
            fec.Add(new TextFormElement("postName", "Post Name to display photo gallery on", "The post name of the post that the gallery should be displayed on. It will be rendered below the post body."));
            fec.Add(new TextFormElement("width", "Photo Gallery Width", "The width  of the photo gallery control in pixels or a percentage (i.e. 400 or 100%)."));
            fec.Add(new TextFormElement("height", "Photo Gallery Height", "The height of the photo gallery control in pixels or a percentage (i.e. 400 or 100%)."));

            fec.Add(new MessageElement("advancedOptions", "<br /><strong>Advanced Options</strong>", "Below are advanced options if you want greater control over how the gallery is rendered."));
            ListFormElement lfe = new ListFormElement("slideTransition", "Transition Effect", "Select the type of transition effect to use between photos");
            lfe.Add(new ListItemFormElement("Cross Fade", "CrossFadeTransition", true));
            lfe.Add(new ListItemFormElement("Circle Out", "CircleOutTransition", true));
            lfe.Add(new ListItemFormElement("Slide Left", "SlideLeftTransition", true));
            lfe.Add(new ListItemFormElement("Slide Down", "SlideDownTransition", true));
            lfe.Add(new ListItemFormElement("Wipe Right", "WipeRightTransition", true));
            fec.Add(lfe);
            fec.Add(new TextAreaFormElement("modulesXML", "Modules XML Configuration", "Enter the XML for the modules you wish to use.", 4));
            fec.Add(new TextFormElement("startAlbum", "Album Number to Auto Start", "By default it will display a list of albums when it loads. If you want to automatically start displaying pictures from an album, enter the number below (starting with 0) and change the <i>initialAlbumView</i> option in the Modules XML box above to have a value of \"false\"."));
        }

        internal static bool ConvertStringToBool(string checkValue)
        {
            if (string.IsNullOrEmpty(checkValue))
                return false;
            else if (checkValue == "checked" || checkValue == "on")
                return true;
            else
                return bool.Parse(checkValue);
        }

        internal static void RenderSlideShowScript(StringBuilder sb, TemplatedThemePage ttp, string postName)
        {
            if (ttp != null && ttp.PostId > 0)
            {
                Post post = Post.GetCachedPost(ttp.PostId);
                if (post != null && post.Name.ToLower() == postName.ToLower())
                {
                    string slJS = ttp.ClientScript.GetWebResourceUrl(typeof(Graffiti.Slide.Show.PluginHelper), "Graffiti.Slide.Show.Silverlight.js");
                    string ssJS = ttp.ClientScript.GetWebResourceUrl(typeof(Graffiti.Slide.Show.PluginHelper), "Graffiti.Slide.Show.SlideShow.js");

                    sb.AppendFormat("<script type=\"text/javascript\" src=\"{0}\" ></script>\n", slJS);
                    sb.AppendFormat("<script type=\"text/javascript\" src=\"{0}\" ></script>\n", ssJS);
                }
            }
        }

        internal static string RenderTransition(string name)
        {
            switch (name)
            {
                case "CrossFadeTransition":
                    return "<transition type=\"FadeTransition\" name=\"CrossFadeTransition\" />\n";
                    break;
                case "CircleOutTransition":
                    return "<transition type=\"ShapeTransition\" name=\"CircleOutTransition\" />\n";
                    break;
                case "SlideLeftTransition":
                    return "<transition type=\"SlideTransition\" name=\"SlideLeftTransition\" />\n";
                    break;
                case "SlideDownTransition":
                    return "<transition type=\"SlideTransition\" name=\"SlideDownTransition\">\n"
                        + "<option name=\"direction\" value=\"Down\" />\n"
                        + "</transition>\n";
                    break;
                case "WipeRightTransition":
                    return "<transition type=\"WipeTransition\" name=\"WipeRightTransition\">\n"
                        + "<option name=\"direction\" value=\"Right\" />\n"
                        + "</transition>\n";
                    break;
            }

            // Default to CrossFade
            return "<transition type=\"FadeTransition\" name=\"CrossFadeTransition\" />\n";
        }

    }
}
